﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EarringsBackEnd
{
    public partial class frmEarringsSuitability : Form
    {
        private string _keySelected = string.Empty;
        private MasterDataManagement _dtMgmt = null;
        private string[] _strArrHeader = null;
        private List<EarringsProp> _earProp = null;
        private List<PersonProp> _persProp = null;
        private List<PropertiesValue> _propValue = null;
        private DataTable _dtSuitability = null;
        private GroupType _grpType;
        private string _grpName = string.Empty;
        private string _earsKeyId = string.Empty;
        private string _persKeyId = string.Empty;

        private string _shrtERProp = string.Empty;
        private string _shrtPSProp = string.Empty;
        private bool _flagEdit = false;

        private  string[] _gridHeader;
        private DataTable _dtSuitSelect;

        public frmEarringsSuitability()
        {
            InitializeComponent();
        }

        private void frmEarringsSuitability_Load(object sender, EventArgs e)
        {
            this.GetData();
            this.InitializeVariableValue();
        }

        private void GetData()
        {
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _propValue = new List<PropertiesValue>();
            _propValue =(List<PropertiesValue>)_dtMgmt.GetProperties<PropertiesValue>(GlobalValue.GRPNAME_EARR);

            PropertiesValue earVal = new PropertiesValue();
            earVal.KeyId = "0";
            earVal.Name = "--กรุณาเลือกคุณสมบัติต่างหู--";
            _propValue.Insert(0, earVal);

            cboEarProp.DataSource = _propValue;
            cboEarProp.ValueMember = "KeyId";
            cboEarProp.DisplayMember = "Name";  
            cboEarProp.SelectedIndex = 0;

            _propValue = new List<PropertiesValue>();
            _propValue = (List<PropertiesValue>)_dtMgmt.GetProperties<PropertiesValue>(GlobalValue.GRPNAME_PERS);

            PropertiesValue proVal = new PropertiesValue();
            proVal.KeyId = "0";
            proVal.Name = "--กรุณาเลือกลักษณะ--";
            _propValue.Insert(0, proVal);

            cboPersProp.DataSource = _propValue;
            cboPersProp.ValueMember = "KeyId";
            cboPersProp.DisplayMember = "Name";
            cboPersProp.SelectedIndex = 0;            
        }

        private void cboEarProp_SelectedIndexChanged(object sender, EventArgs e)
        {
            _grpType = GroupType.EarringsProp;
            _grpName = GlobalValue.GRPNAME_EARR;
            _flagEdit = false;

            if (cboEarProp.SelectedIndex > 0)
            {
                PropertiesValue earVal = cboEarProp.SelectedItem as PropertiesValue;
                _earsKeyId = earVal.KeyId.ToString();

                this.GetSuitabilty(GroupType.EarringsProp);                         
            }
        }
        
        private void LoadEarringsPropDetail()
        {
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _earProp = new List<EarringsProp>();
            _earProp = (List<EarringsProp>)_dtMgmt.GetPropertiesDetail<EarringsProp>(cboEarProp.Text);
        }

        private void cboPersProp_SelectedIndexChanged(object sender, EventArgs e)
        {
            _grpType = GroupType.PersonProp;
            _grpName = GlobalValue.GRPNAME_PERS;
            _flagEdit = false;

            if (cboPersProp.SelectedIndex > 0)
            {
                PropertiesValue perVal = cboPersProp.SelectedItem as PropertiesValue;
                _persKeyId = perVal.KeyId.ToString();

                this.GetSuitabilty(GroupType.PersonProp);                
            }
        }

        private void LoadPersonPropDetail()
        {
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _persProp = new List<PersonProp>();
            _persProp = (List<PersonProp>)_dtMgmt.GetPropertiesDetail<PersonProp>(cboPersProp.Text);
        }

        private void ResultEarringsPersonSuitability()
        {
            if (cboEarProp.SelectedIndex > 0 && cboPersProp.SelectedIndex > 0)
            {
                for (int i = dtgvSuitability.Columns.Count - 1; i >= 0; i--)
                {
                    dtgvSuitability.Columns.RemoveAt(i);
                }

                _dtSuitability = new DataTable();
                _dtSuitability.Columns.Add("No");
                _dtSuitability.Columns.Add("SuitId");
                _dtSuitability.Columns.Add("EarsProp");
                _dtSuitability.Columns.Add("PersProp");
                _dtSuitability.Columns.Add("EarsConfId");
                _dtSuitability.Columns.Add("PersConfId");

                foreach (EarringsProp earp in _earProp)
                {
                    foreach (PersonProp persp in _persProp)
                    {
                        DataRow row = _dtSuitability.NewRow();
                        row["No"] = _dtSuitability.Rows.Count+1;
                        row["SuitId"] = earp.KeyId + '-' + persp.KeyId; ;
                        row["EarsProp"] = earp.Name;
                        row["PersProp"] = persp.Name;
                        row["EarsConfId"] = earp.ConfigId;
                        row["PersConfId"] = persp.ConfigId;
                        _dtSuitability.Rows.Add(row);
                    }
                }
                dtgvSuitability.DataSource = null;
                dtgvSuitability.DataSource = _dtSuitability;

                this.InitializeDataGridView();
            }
        }
        
        private void InitializeVariableValue()
        {
            if (cboEarProp.SelectedIndex > 0 && cboPersProp.SelectedIndex > 0)
            {
                _gridHeader = new string[] { "รหัสความเหมาะสม", cboEarProp.Text, cboPersProp.Text, "รหัสคุณสมบัติ", "รหัสลักษณะ", "คะแนน" };
                _strArrHeader = _gridHeader;
            }
            else
            {
                _strArrHeader = GlobalValue.gbStrArrSUITHeader;
            }

            _dtSuitSelect = new DataTable();
            _dtSuitSelect.Columns.Add("RatingValue", typeof(int));
            _dtSuitSelect.Columns.Add("RatingName", typeof(string));

            _dtSuitSelect.Rows.Add(0, "ไม่แนะนำ");
            _dtSuitSelect.Rows.Add(1, "ใส่ได้");
            _dtSuitSelect.Rows.Add(3, "แนะนำให้ใส่");

        }

        private void InitializeDataGridView()
        {
            if (cboEarProp.SelectedIndex > 0 && cboPersProp.SelectedIndex > 0)
            {
                _gridHeader = new string[] { "ลำดับที่","รหัสความเหมาะสม", cboEarProp.Text, cboPersProp.Text, "รหัสคุณสมบัติ", "รหัสลักษณะ", "คะแนน" };
            }
            _strArrHeader = _gridHeader;
            //Set Header Grid
            for (int intIndex = 0; intIndex < dtgvSuitability.Columns.Count; intIndex++)
            {
                dtgvSuitability.Columns[intIndex].HeaderText = _strArrHeader[intIndex];
            }
            
            //คะแนนความหมาะสม แบบคีย์ตัวเลข
            //TNumEditDataGridViewColumn ratio = new TNumEditDataGridViewColumn();
            //ratio.DataPropertyName = "Ratio";
            //ratio.Width = 70;
            //ratio.HeaderText = "คะแนน";
            //dtgvSuitability.Columns.Add(ratio);

            //คะแนนความหมาะสม แบบเลือก Combo
            DataGridViewComboBoxColumn rating = new DataGridViewComboBoxColumn();
            rating.DataPropertyName = "Rating";
            rating.Width = 120;
            rating.HeaderText = "คะแนน";
            rating.DataSource = _dtSuitSelect;
            rating.DisplayMember = "RatingName";
            rating.ValueMember = "RatingValue";
            dtgvSuitability.Columns.Add(rating);

            //ลำดับที่
            dtgvSuitability.Columns[0].Width = 70;
            dtgvSuitability.Columns[0].ReadOnly = true;
            //รหัสความเหมาะสม
            dtgvSuitability.Columns[1].Width = 150;
            dtgvSuitability.Columns[1].Visible = false;
            dtgvSuitability.Columns[1].ReadOnly = true;
            //ชื่อคุณสมบัติต่างหู
            dtgvSuitability.Columns[2].Width = 150;
            dtgvSuitability.Columns[2].ReadOnly = true;
            //ชื่อคุณลักษณะบุคคล
            dtgvSuitability.Columns[3].Width = 120;
            dtgvSuitability.Columns[3].ReadOnly = true;
            //รหัสคุณสมบัติต่างหู
            dtgvSuitability.Columns[4].Visible = false;
            dtgvSuitability.Columns[4].ReadOnly = true;
            //รหัสคุณลักษณะบุคคล
            dtgvSuitability.Columns[5].Visible = false;
            dtgvSuitability.Columns[5].ReadOnly = false;
            

            DataGridViewCellStyle style = dtgvSuitability.ColumnHeadersDefaultCellStyle;
            style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(dtgvSuitability.Font, FontStyle.Regular);
        }

        private void dtgvSuitability_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //if (e.ColumnIndex == 0)
            //{
            //    e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //}
            ///////////////////
            //if(_flagEdit)
            //{
            //    dtgvSuitability.Columns[0].Width = 120;
            //    dtgvSuitability.Columns[0].ReadOnly = true;
            //    dtgvSuitability.Columns[1].Width = 150;
            //    dtgvSuitability.Columns[1].ReadOnly = true;
            //    dtgvSuitability.Columns[2].Width = 150;
            //    dtgvSuitability.Columns[2].ReadOnly = true;

            //    dtgvSuitability.Columns[3].Visible = false;
            //    dtgvSuitability.Columns[3].ReadOnly = true;

            //    dtgvSuitability.Columns[4].Visible = false;
            //    dtgvSuitability.Columns[4].ReadOnly = true;

            //    dtgvSuitability.Columns[5].ReadOnly = false;
            //    dtgvSuitability.Columns[5].Visible = true;
            //}

            //DataGridViewCellStyle style = dtgvSuitability.ColumnHeadersDefaultCellStyle;
            //style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //style.BackColor = Color.Navy;

            //if (dtgvSuitability.Rows[e.RowIndex].Cells[5].Value == null)
            //{
            //    dtgvSuitability.Columns[5].DefaultCellStyle.BackColor = Color.Wheat;
            //}
            //else
            //{
            //    dtgvSuitability.Columns[5].DefaultCellStyle.BackColor = Color.Transparent;
            //}
        }

        private void dtgvSuitability_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            //if (e.ColumnIndex == 5)
            //{
            //    e.Cancel = false;
            //}
        }

        private void btnSave_Click(object sender, EventArgs e)
        {            
            string code = string.Empty;
            string conf_1 = string.Empty;
            string conf_2 = string.Empty;
            string rating = string.Empty;

            foreach (DataGridViewRow myRow in dtgvSuitability.Rows)
            {
                if (!myRow.IsNewRow)
                {
                   

                    //DataGridViewComboBoxCell cell = myRow.Cells[6] as DataGridViewComboBoxCell;
                    //rating = cell.Value.ToString();
                    
                    if (!_flagEdit)
                    {
                        code = myRow.Cells[1].Value.ToString();
                        conf_1 = myRow.Cells[4].Value.ToString();
                        conf_2 = myRow.Cells[5].Value.ToString();
                        rating = myRow.Cells[6].Value.ToString();

                        this.SaveSuitabiltyData(code, conf_1, conf_2, rating);

                        _flagEdit = false;
                    }
                    else
                    {
                        //code = myRow.Cells[2].Value.ToString();
                        conf_1 = myRow.Cells[3].Value.ToString();
                        conf_2 = myRow.Cells[4].Value.ToString();
                        rating = myRow.Cells[6].Value.ToString();

                        this.UpdateSuitabiltyData(conf_1, conf_2, rating);
                    }
                }
            }

            MessageBox.Show("บันทึกเรียบร้อย");
        }

        private void SaveSuitabiltyData(string code, string conf_Id1, string conf_Id2, string rating)
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@SuitCode", code) ,
                    new SqlParameter("@Config_1", conf_Id1),
                    new SqlParameter("@Config_2", conf_Id2),
                    new SqlParameter("@Short_Type1", _earsKeyId),
                    new SqlParameter("@Short_Type2", _persKeyId),
                    new SqlParameter("@Rating", rating)
                };
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _dtMgmt.SaveEarringsSuitability(sqlParams);
        }

        private void UpdateSuitabiltyData(string conf_Id1, string conf_Id2, string rating)
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@Config_1", conf_Id1),
                    new SqlParameter("@Config_2", conf_Id2),
                    new SqlParameter("@Short_Type1", _earsKeyId),
                    new SqlParameter("@Short_Type2", _persKeyId),
                    new SqlParameter("@Rating", rating)
                };
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _dtMgmt.UpdateEarringsSuitability(sqlParams);
        }

        private void GetSuitabilty(GroupType type)
        {
            if (cboEarProp.SelectedIndex > 0 && cboPersProp.SelectedIndex > 0)
            {
                _gridHeader = new string[] { "ลำดับที่", cboEarProp.Text, cboPersProp.Text, "รหัสคุณสมบัติ", "รหัสลักษณะ", "คะแนน" };
               
                SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@Conf_Grp1", _earsKeyId),
                    new SqlParameter("@Conf_Grp2", _persKeyId),
                };
                _dtMgmt = MasterDataManagement.InstanceCreation();
                DataTable dt = new DataTable();
                dt = _dtMgmt.GetEarringsSuitability(sqlParams);

                if (dt.Rows.Count > 0)
                {
                    _flagEdit = true;

                    //Remove All Column in Datagrid
                    if (dtgvSuitability.Columns.Count > 0)
                    {
                        for (int i = dtgvSuitability.Columns.Count - 1; i >= 0; i--)
                        {
                            dtgvSuitability.Columns.RemoveAt(i);
                        }
                    }

                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = dt;
                    bindingSource.ResetBindings(true);
                    dtgvSuitability.DataSource = bindingSource;
                    dtgvSuitability.Refresh();
                                                          
                    //Set Header Grid
                    for (int intIndex = 0; intIndex < dtgvSuitability.Columns.Count; intIndex++)
                    {
                        dtgvSuitability.Columns[intIndex].HeaderText = _gridHeader[intIndex];// GlobalValue.gbStrArrSUITDSHeader[intIndex];
                    }
                    //คะแนนความหมาะสม แบบเลือก Combo
                    DataGridViewComboBoxColumn rating = new DataGridViewComboBoxColumn();
                    rating.DataPropertyName = "Rating";
                    rating.Width = 120;
                    rating.HeaderText = "คะแนน";
                    rating.DataSource = _dtSuitSelect;
                    rating.DisplayMember = "RatingName";
                    rating.ValueMember = "RatingValue";
                    dtgvSuitability.Columns.Add(rating);

                    //ลำดับที่
                    dtgvSuitability.Columns[0].Width = 70;
                    dtgvSuitability.Columns[0].ReadOnly = true;
                    //ชื่อคุณสมบัติต่างหู
                    dtgvSuitability.Columns[1].Width = 150;
                    dtgvSuitability.Columns[1].ReadOnly = true;
                    //ชื่อคุณลักษณะบุคคล
                    dtgvSuitability.Columns[2].Width = 150;
                    dtgvSuitability.Columns[2].ReadOnly = true;
                    //รหัสคุณสมบัติต่างหู
                    dtgvSuitability.Columns[3].Visible = false;
                    dtgvSuitability.Columns[3].ReadOnly = true;
                    //รหัสคุณลักษณะบุคคล
                    dtgvSuitability.Columns[4].Visible = false;
                    dtgvSuitability.Columns[4].ReadOnly = true;
                    //คะแนนจาก Data Base
                    dtgvSuitability.Columns[5].Visible = false;
                    dtgvSuitability.Columns[5].ReadOnly = true;

                }
                else
                {
                    this.LoadEarringsPropDetail();
                    this.LoadPersonPropDetail();
                    this.ResultEarringsPersonSuitability();
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            cboEarProp.SelectedIndex = 0;
            cboPersProp.SelectedIndex = 0;

            dtgvSuitability.Rows.Clear();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            cboEarProp.SelectedIndex = 0;
            cboPersProp.SelectedIndex = 0;
            dtgvSuitability.Rows.Clear();

            this.Dispose();
            this.Close();
        }

    }
}
